home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / ptv1n1.arc / TABEXP.C < prev    next >
Text File  |  1990-06-08  |  958b  |  42 lines

  1. /* Listing 1. C program to expand tabs in a file */
  2.  
  3. #include <stdio.h>
  4.  
  5. FILE *f;           /* input file stream              */
  6. int col = 0;       /* current column of current line */
  7. int tabsize = 8;   /* given tab size                 */
  8. int tabcnt = 0;    /* amount of spaces left          */
  9.  
  10. int tabexp_get(void)
  11. /* Gets a character from the input file, */
  12. /* expanding tabs along the way          */
  13. {
  14.   int c;
  15.  
  16.   if (tabcnt) {  /* currently expanding tab */
  17.      tabcnt--; col++;
  18.      c = ' ';
  19.   }
  20.   else c = fgetc(f);
  21.  
  22.   if (c == 0x09) { /* tab handler */
  23.      tabcnt = tabsize - (col % tabsize);
  24.      return tabexp_get();
  25.   }
  26.   else {
  27.     if (c == '\n') col = 0; else col++;
  28.   }
  29.   return c;
  30. }
  31.  
  32. main()
  33. {
  34.   int c;
  35.  
  36.   f = stdin; /* default to stdin for input file */
  37.   do {       /* loop until no more characters in input */
  38.     c = tabexp_get();
  39.     if (c == -1) break; else putc(c, stdout);
  40.   } while(1);
  41. }
  42.